package com.connector.base.message.model;

import com.alibaba.fastjson.JSONObject;
import com.connector.base.connection.Connection;
import com.connector.base.protocol.Command;
import com.connector.base.protocol.Packet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HandshakeCheckMessage extends JsonBodyMessage
{
    private static final Logger log;
    private String msg;

    public HandshakeCheckMessage(final Connection connection) {
        super(new Packet(Command.HANDSHAKECHECK.cmd, (byte)0, genSessionId()), connection);
    }

    public HandshakeCheckMessage(final Packet packet, final Connection connection) {
        super(packet, connection);
    }

    @Override
    public void jsonToFields(final JSONObject jsonBody) {
        this.msg = (jsonBody.containsKey((Object)"msg") ? jsonBody.getString("msg") : null);
    }

    @Override
    public JSONObject fieldToJson() {
        final JSONObject resultObj = new JSONObject();
        resultObj.put("message", (Object)this.msg);
        return resultObj;
    }

    public static HandshakeMessage buildResponse(final BaseMessage src) {
        HandshakeMessage response = null;
        if (0 == src.packet.getType()) {
            response = new HandshakeMessage(new Packet(Command.HANDSHAKECHECK.cmd, (byte)1, src.packet.getSessionId()), src.connection);
        }
        else if (2 == src.packet.getType()) {
            response = new HandshakeMessage(new Packet(Command.HANDSHAKECHECK.cmd, (byte)4, src.packet.getSessionId()), src.connection);
        }
        return response;
    }

    protected static int genSessionId() {
        return HandshakeCheckMessage.ID_SEQ.incrementAndGet();
    }

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(final String msg) {
        this.msg = msg;
    }

    @Override
    public String toString() {
        return "HandshakeCheckMessage(msg=" + this.getMsg() + ")";
    }

    public HandshakeCheckMessage() {
    }

    static {
        log = LoggerFactory.getLogger((Class) HandshakeCheckMessage.class);
    }
}
